#pragma once

#include <iostream>
#include <fstream>
#include <string>

class Util
{
public:
    // 
    static bool ReadFileContent(const std::string& filename, std::string *out)
    {
        // version1：默认是以文本的方式读取文件的，图片是二进制的
        // std::ifstream in(fliename);
        // if(!in.is_open())
        // {
        //     return false;
        // }

        // std::string line;
        // while(std::getline(in, line))
        // {
        //     *out += line;
        // }

        // in.close();

        // version：以二进制的方式读取文件
        int filesize = FileSize(filename);
        if(filesize > 0)
        {
            std::ifstream in(filename);
            if(!in.is_open())
                return false;
            out->resize(filesize);
            in.read((char*)(out->c_str()), filesize);
            in.close();
        }
        else
        {
            return false;
        }

        return true;
    }

    static bool ReadOneLine(std::string& bigstr, std::string *out, const std::string& sep/*"\r\n"*/)
    {
        size_t pos = bigstr.find(sep);
        if(pos == std::string::npos)
        {
            return false;
        }

        *out = bigstr.substr(0, pos);
        bigstr.erase(0, pos + sep.length());

        return true;
    }

    static int FileSize(const std::string& filename)
    {
        std::ifstream in(filename, std::ios::binary);
        if(!in.is_open())
        {
            return -1;
        }

        in.seekg(0, in.end);
        int size = in.tellg();
        in.seekg(0, in.beg);
        in.close();

        return size;
    }
};